package db.ibatis;

/**
 * Created by boot on 9/9/15.
 */
public class Page {

    public Page() {
    }

    public Page(int reqPage) {
        this.currentPage = reqPage;
    }

    public Page(int reqPage, int pageSize) {
        this.currentPage = reqPage;
        this.pageSize = pageSize;
    }

    // Current page of the request.
    private int currentPage;

    // Page size of the request.
    private int pageSize = 5;

    // Calculated the index of row count.
    private int dbIndex;

    // Total row count of the query result.
    private long totalNumber;

    // Calculated total page of the query result.
    private long totalPage;


    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(int dbIndex) {
        this.dbIndex = dbIndex;
    }

    public long getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(long totalNumber) {
        this.totalNumber = totalNumber;
        calculate();
    }

    public long getTotalPage() {
        return totalPage;
    }


    private void calculate() {
        dbIndex = (currentPage - 1) * pageSize;
        totalPage = (totalNumber + 1) / pageSize;
        if (totalPage < 1)
            totalPage = 1;
        if (currentPage < 1)
            currentPage = 1;
        if (currentPage > totalPage)
            currentPage = (int) totalPage;
    }
}
